using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class VampireRider : Microsoft.Xna.Framework.Game
    {
        public static GraphicsDeviceManager graphics;
        private int BackBufferWidth = 800;
        private int BackBufferHeight = 600; 
        public static SpriteBatch spriteBatch;

        public static int menuPosition=0;

        public static SpriteFont[] fonts = new SpriteFont[10];
        public static Texture2D[] sprites = new Texture2D[50];
        public static int frames;
        public static float delay = 200;
        public static float elapsed;
        public static int playerAnimState = 0;

        MenuScreen mainMenu = new MenuScreen();
        MainScreen mainScreen = new MainScreen();
        Player gracz = new Player(250,250);
        public static Enemy[] enemyTab = 
        {
            new Enemy(1,400,400),
            new Enemy(2,100,200),
            new Enemy(3,700,300),
        };
              
        Item item = new Item();
        Weapon weapon1 = new Weapon();
        FirstAid health1 = new FirstAid();

        public static MouseState CurrentMouseState;
        public static MouseState PreviousMouseState;
        KeyboardState CurrentKeyboardState;
        KeyboardState PreviousKeyboardState;

        
        public VampireRider()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = BackBufferWidth;
            graphics.PreferredBackBufferHeight = BackBufferHeight; 
        }


        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {

            // TODO: Add your initialization logic here
             base.Initialize();    
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);


            fonts[0] = Content.Load<SpriteFont>("Content/menu");
            fonts[1] = Content.Load<SpriteFont>("Content/arial");//
            sprites[0] = Content.Load<Texture2D>("Content/main_menu");//main menu
            sprites[1] = Content.Load<Texture2D>("Content/gui");//menu screen
            sprites[2] = Content.Load<Texture2D>("Content/Sprite");//player
            sprites[3] = Content.Load<Texture2D>("Content/cola");//enemy
            sprites[4] = Content.Load<Texture2D>("Content/crosshair");//crosshair
            sprites[5] = Content.Load<Texture2D>("Content/bez_broni");
            CurrentMouseState = Mouse.GetState();
            PreviousMouseState = CurrentMouseState;

            

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            CurrentKeyboardState = Keyboard.GetState();
            CurrentMouseState = Mouse.GetState();

            elapsed += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            if (elapsed >= delay)
            {
                if (frames >= 3)
                {
                    frames = 0;
                }
                else
                {
                    frames++;
                }
                elapsed = 0;
            }
            
            // Allows the game to exit
            if (CurrentKeyboardState.IsKeyDown(Keys.Escape))
                menuPosition=0;
            if (CurrentKeyboardState.IsKeyDown(Keys.Space) && PreviousKeyboardState.IsKeyUp(Keys.Space))
            {
                if (menuPosition == 1)
                {
                    menuPosition = 6;
                }
                else
                    menuPosition = 1;
           }
            if (CurrentKeyboardState.IsKeyDown(Keys.Up) && menuPosition==1)
            {
                gracz.coord.Y = (int)gracz.coord.Y - 1;
            }
            if(CurrentKeyboardState.IsKeyDown(Keys.Down) && menuPosition==1)
            {
                gracz.coord.Y = (int)gracz.coord.Y + 1;
            }
            if (CurrentKeyboardState.IsKeyDown(Keys.Left) && menuPosition == 1)
            {
                gracz.coord.X = (int)gracz.coord.X - 1;
            }
            if (CurrentKeyboardState.IsKeyDown(Keys.Right) && menuPosition == 1)
            {
                gracz.coord.X = (int)gracz.coord.X + 1;
            }
            
                

            if (CurrentKeyboardState.IsKeyDown(Keys.F)) 
            { 
                graphics.ToggleFullScreen();
            }

            if (CurrentKeyboardState.IsKeyDown(Keys.K) && PreviousKeyboardState.IsKeyUp(Keys.K))
            {
                enemyTab[0].HP -= 10;
            }

            // TODO: Add your update logic here
            this.IsMouseVisible = true;
            PreviousMouseState = CurrentMouseState;
            PreviousKeyboardState = CurrentKeyboardState;

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
           
            GraphicsDevice.Clear(Color.CornflowerBlue);
            switch(menuPosition)
            {
                case 0:
                    mainMenu.Draw(spriteBatch,CurrentMouseState,sprites[0],fonts[0],ref menuPosition);
                    break;
                case 1:
                    mainScreen.Draw(spriteBatch, CurrentMouseState, sprites[1], fonts[0], ref menuPosition);
                    gracz.Go(spriteBatch,CurrentKeyboardState, sprites[5],50, frames, gracz,ref playerAnimState);
                    gracz.ShowInfo(GraphicsDevice,spriteBatch,fonts[0]);

                    for (int i = 0; i < enemyTab.Length; i++)
                    {
                        if (enemyTab[i].HP >= 0)
                        {
                            enemyTab[i].DrawObject(spriteBatch, sprites[3]);
                            enemyTab[i].ShowInfo(GraphicsDevice, spriteBatch, fonts[0]);
                        }

                    }
                    mainScreen.Aim(spriteBatch, CurrentMouseState, sprites[4], enemyTab);

                    break;
                case 2:
                    mainMenu.NewGame(spriteBatch,sprites[0],fonts[0]);
                    break;
                case 3:
                    mainMenu.SaveGame(spriteBatch, sprites[0], fonts[0]);
                    break;
                case 4:
                    mainMenu.LoadGame(spriteBatch, sprites[0], fonts[0]);
                    break;
                case 5:
                    mainMenu.ExitGame(spriteBatch,CurrentMouseState,sprites[0],fonts[0],menuPosition);
                    break;
                case 6:
                    mainMenu.PauseGame(spriteBatch,sprites[0],fonts[0]);
                    break;
            }

           // TODO: Add your drawing code here
            spriteBatch.Begin();
            spriteBatch.DrawString(fonts[1], "Build 20131130", mainMenu.textVector[11], Color.White);
            spriteBatch.End();
            base.Draw(gameTime);


        }
    }
}
